﻿Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid
Imports System.ComponentModel
Imports DTO
Imports BUS
Imports DevExpress.XtraTreeList.Nodes
Imports DevExpress.XtraTreeList
Imports DevExpress.Utils.Menu

Public Class ucPhanQuyen
    Private dsChucNang As DataTable
    Private dsNhomND As BindingList(Of NhomNDDTO)
    Private dsNguoiDung As DataTable
    Private _load As Boolean = False
    Private _NhomND As Boolean
    Private pq As New PhanQuyenNhonNDDTO
    Private pqQLND As PhanQuyenNhonNDDTO
    Private pqQLNhomND As PhanQuyenNhonNDDTO
    Sub New()

        InitializeComponent()
        ' This line of code is generated by Data Source Configuration Wizard
        'NguoidungTableAdapter1.Fill(ND_KH1.NGUOIDUNG)

    End Sub

    Private Sub btnPhanQuyen_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThemVaiTro.ItemClick
        If pqQLNhomND.TruyCap = True Then
            Dim frm As New frmThemVaiTro("",pqQLNhomND)
            AddHandler frm.suKienTaiLaiUC, AddressOf suKienTaiLaiUC
            frm.ShowDialog()
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Bạn không có quyền vào mục này", "Lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Stop)
        End If
    End Sub

    Public Sub suKienTaiLaiUC()
        layDuLieuVaiTroVaND()
    End Sub

    Private Sub btnThemNG_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThemNguoiDung.ItemClick
        If pqQLND.TruyCap = True Then
            Dim frm As New frmThemNguoiDung("",pqQLND)
            AddHandler frm.suKienTaiLaiUC, AddressOf suKienTaiLaiUC
            frm.ShowDialog()
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Bạn không có quyền vào mục này", "Lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Stop)
        End If
    End Sub

    Private Sub btnSua_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnSua.ItemClick
        Try
            If _NhomND = True Then
                'If grvNhom.IsRowSelected(0) = True Then
                If pqQLNhomND.Sua = True Then
                    If grvNhom.GetSelectedRows.Length > 0 Then
                        Dim index As Integer = grvNhom.GetSelectedRows()(0)
                        Dim maNhom As String = grvNhom.GetRowCellValue(index, GridColumn1).ToString
                        If Not maNhom Is Nothing Then
                            Dim frm As New frmThemVaiTro(maNhom, pqQLNhomND)
                            AddHandler frm.suKienTaiLaiUC, AddressOf suKienTaiLaiUC
                            frm.ShowDialog()
                            Return
                        End If
                    End If
                    

                Else
                    DevExpress.XtraEditors.XtraMessageBox.Show("Bạn không có quyền vào mục này", "Lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                End If
                'End If

            Else
                If pqQLND.Sua = True Then
                    For Each node As TreeListNode In treeNDVaiTro.Nodes
                        For Each nodeChild As TreeListNode In node.Nodes
                            If nodeChild.Selected = True Then
                                Dim frm As New frmThemNguoiDung(nodeChild.GetValue(0).ToString, pqQLND)
                                AddHandler frm.suKienTaiLaiUC, AddressOf suKienTaiLaiUC
                                frm.ShowDialog()
                                Return
                            End If
                        Next
                    Next

                Else
                    DevExpress.XtraEditors.XtraMessageBox.Show("Bạn không có quyền vào mục này", "Lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                End If



            End If
        Catch ex As Exception
            If pqQLND.Sua = True Then
                For Each node As TreeListNode In treeNDVaiTro.Nodes
                    For Each nodeChild As TreeListNode In node.Nodes
                        If nodeChild.Selected = True Then
                            Dim frm As New frmThemNguoiDung(nodeChild.GetValue(0).ToString, pqQLND)
                            AddHandler frm.suKienTaiLaiUC, AddressOf suKienTaiLaiUC
                            frm.ShowDialog()
                        End If
                    Next
                Next
            Else
                DevExpress.XtraEditors.XtraMessageBox.Show("Bạn không có quyền vào mục này", "Lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            End If
        End Try
    End Sub

    Private Sub ucPhanQuyen_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        pq = ChucNangBUS.layQuyenCuaND(frmMain.maND, 5)
        pqQLND = ChucNangBUS.layQuyenCuaND(frmMain.maND, 6)
        pqQLNhomND = ChucNangBUS.layQuyenCuaND(frmMain.maND, 7)
        If pq.TruyCap = True Then
            suKienTaiLaiUC()
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Bạn không có quyền vào mục này", "Lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Stop)
        End If

    End Sub

    Private Sub layDuLieuPhanQuyen(maND As String)
        dsChucNang = ChucNangBUS.layChucNang(maND)
        For Each row As DataRow In dsChucNang.Rows
            Dim node1 As TreeListNode = Nothing
            node1 = treePhanQuyen.FindNodeByID(Integer.Parse(row("MaCha").ToString) - 1)
            If node1 Is Nothing Then
                treePhanQuyen.AppendNode(New Object() {row("TenChucNang").ToString, row("TatCa"), row("TruyCap"), row("Them"), row("Xoa"), _
                                                       row("Sua"), row("In"), row("Nhap"), row("Xuat")}, Integer.Parse(row("MaChucNang").ToString) - 1)
            Else
                treePhanQuyen.AppendNode(New Object() {row("TenChucNang").ToString, row("TatCa"), row("TruyCap"), row("Them"), row("Xoa"), _
                                                       row("Sua"), row("In"), row("Nhap"), row("Xuat")}, node1)
            End If
        Next
    End Sub

    Private Sub treePhanQuyen_CellValueChanging(sender As Object, e As CellValueChangedEventArgs) Handles treePhanQuyen.CellValueChanging
        Dim colIndex As Integer
        For i = 0 To treePhanQuyen.Columns.Count - 1
            If treePhanQuyen.Columns(i).Name = e.Column.Name Then
                colIndex = i
            End If
        Next
        If e.Column.Name = "colTaTCa" Then
            e.Node.SetValue(colIndex, e.Value)
            duyetNodeCotTatCa(e.Node, colIndex, e.Value)
        Else
            e.Node.SetValue(colIndex, e.Value)
            duyetnode(e.Node, colIndex, e.Value)
        End If
    End Sub

    Private Delegate Sub LoaDLVT()

    Private Sub layDuLieuVaiTroVaND()
        If Me.treeNDVaiTro.InvokeRequired() Then
            Me.Invoke(New LoaDLVT(AddressOf layDuLieuVaiTroVaND))
        Else
            Me.treeNDVaiTro.Nodes.Clear()
            dsNhomND = NhomNDBUS.layDsNhomND()
            For i = 0 To dsNhomND.Count - 1
                treeNDVaiTro.AppendNode(New Object() {dsNhomND(i).TenNhom.ToString}, i, dsNhomND(i).MaNhom.ToString + "_" + "Nhom")
            Next
            For i = 0 To dsNhomND.Count - 1

                For j = 0 To dsNhomND(i).dsNguoiDung.Count - 1
                    Dim node = FirstTagValueInNode(treeNDVaiTro.Nodes, dsNhomND(i).dsNguoiDung(j).MaNhom.ToString + "_" + "Nhom")
                    If Not node Is Nothing Then
                        treeNDVaiTro.AppendNode(New Object() {dsNhomND(i).dsNguoiDung(j).TaiKhoan.ToString.Replace(" ", "")}, node, dsNhomND(i).dsNguoiDung(j).MaNguoiDung.ToString + "_ND")

                    End If
                Next
            Next
            treeNDVaiTro.ExpandAll()
            treeNDVaiTro.Nodes(0).Selected = True
            Dim maNhom As String = treeNDVaiTro.Nodes(0).Tag.ToString.Split(Char.Parse("_"))(0)
            loadLoadDuLieuNhomNguoiDung(maNhom)
            layDuLieuPhanQuyen(maNhom)
            _load = True
        End If
      
    End Sub

    Public Function FirstTagValueInNode(ByVal nodes As DevExpress.XtraTreeList.Nodes.TreeListNodes, ByVal tagSearch As String) As TreeListNode
        For Each node As DevExpress.XtraTreeList.Nodes.TreeListNode In nodes
            If node.Tag.ToString = tagSearch Then
                Return node
            End If
        Next
        Return Nothing
    End Function

    Private Sub treeNDVaiTro_CellValueChanging(sender As Object, e As CellValueChangedEventArgs) Handles treeNDVaiTro.CellValueChanging
        MessageBox.Show(e.Node.GetValue(0).ToString)
    End Sub

    Private Sub loadLoadDuLieuNhomNguoiDung(manhom As String)
        GridControl1.DataSource = DBNull.Value
        Dim nhom As New BindingList(Of NhomNDDTO)
        nhom.Add(dsNhomND.ToList().Find(Function(values As NhomNDDTO)
                                            Return values.MaNhom = manhom
                                        End Function))

        GridControl1.DataSource = nhom
        grvNhom.ExpandMasterRow(0)
    End Sub

    Private Sub duyetnode(node As TreeListNode, colName As Object, valuse As Object)
        If node.HasChildren Then
            For Each nodeChild As TreeListNode In node.Nodes
                nodeChild.SetValue(colName, valuse)
                duyetnode(nodeChild, colName, valuse)
            Next
        End If
    End Sub

    Private Sub duyetNodeCotTatCa(node As TreeListNode, colIndex As Integer, valuse As Object)
        For i = colIndex To treePhanQuyen.Columns.Count - 1
            node.SetValue(i, valuse)
        Next
        If node.HasChildren Then
            For Each nodeChild As TreeListNode In node.Nodes
                For i = colIndex To treePhanQuyen.Columns.Count - 1
                    nodeChild.SetValue(i, valuse)
                    duyetnode(nodeChild, i, valuse)
                Next
            Next
        End If
    End Sub


    Private Sub treeNDVaiTro_MouseUp(sender As Object, e As MouseEventArgs) Handles treeNDVaiTro.MouseUp, treePhanQuyen.MouseUp, GridControl1.MouseUp
        If e.Button = MouseButtons.Right Then
            PopupMenu1.ShowPopup(Control.MousePosition)
        End If

    End Sub

    Private Sub ucPhanQuyen_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown, treeNDVaiTro.KeyDown
        If e.KeyCode = Keys.N AndAlso e.Modifiers = Keys.ControlKey Then
            Dim frm As New frmThemVaiTro("", pqQLNhomND)
            AddHandler frm.suKienTaiLaiUC, AddressOf suKienTaiLaiUC
            frm.ShowDialog()
        End If
    End Sub

    Private Sub treeNDVaiTro_MouseClick(sender As Object, e As MouseEventArgs) Handles treeNDVaiTro.MouseClick
        If e.Button = MouseButtons.Left Then
            Dim tl As TreeList = TryCast(sender, TreeList)
            Dim hitInfo As TreeListHitInfo = tl.CalcHitInfo(e.Location)
            If Not hitInfo.Node Is Nothing Then
                tl.FocusedNode = hitInfo.Node
                dsChucNang.Clear()
                treePhanQuyen.Nodes.Clear()
                If tl.FocusedNode.Tag.ToString.Split(Char.Parse("_"))(1) = "Nhom" Then

                    dsChucNang = ChucNangBUS.layChucNang(tl.FocusedNode.Tag.ToString)
                    'treePhanQuyen.DataSource = dsChucNang
                    For Each row As DataRow In dsChucNang.Rows
                        Dim node1 As TreeListNode = Nothing
                        node1 = treePhanQuyen.FindNodeByID(Integer.Parse(row("MaCha").ToString) - 1)
                        If node1 Is Nothing Then
                            treePhanQuyen.AppendNode(New Object() {row("TenChucNang").ToString, row("TatCa"), row("TruyCap"), row("Them"), row("Xoa"), _
                                                                   row("Sua"), row("In"), row("Nhap"), row("Xuat")}, Integer.Parse(row("MaChucNang").ToString) - 1)
                        Else
                            treePhanQuyen.AppendNode(New Object() {row("TenChucNang").ToString, row("TatCa"), row("TruyCap"), row("Them"), row("Xoa"), _
                                                                   row("Sua"), row("In"), row("Nhap"), row("Xuat")}, node1)
                        End If
                    Next

                    grvNhom.ClearSelection()
                    Dim mNhom As String = tl.FocusedNode.Tag.ToString.Split(Char.Parse("_"))(0)
                    loadLoadDuLieuNhomNguoiDung(mNhom)
                    grvNhom.FocusedRowHandle = 0
                    grvNhom.SelectRow(0)
                    _NhomND = True
                Else
                    dsChucNang = ChucNangBUS.layChucNang(tl.FocusedNode.ParentNode.Tag.ToString)
                    'treePhanQuyen.DataSource = dsChucNang
                    For Each row As DataRow In dsChucNang.Rows
                        Dim node1 As TreeListNode = Nothing
                        node1 = treePhanQuyen.FindNodeByID(Integer.Parse(row("MaCha").ToString) - 1)
                        If node1 Is Nothing Then
                            treePhanQuyen.AppendNode(New Object() {row("TenChucNang").ToString, row("TatCa"), row("TruyCap"), row("Them"), row("Xoa"), _
                                                                   row("Sua"), row("In"), row("Nhap"), row("Xuat")}, Integer.Parse(row("MaChucNang").ToString) - 1)
                        Else
                            treePhanQuyen.AppendNode(New Object() {row("TenChucNang").ToString, row("TatCa"), row("TruyCap"), row("Them"), row("Xoa"), _
                                                                   row("Sua"), row("In"), row("Nhap"), row("Xuat")}, node1)
                        End If
                    Next
                    Dim mNhom As String = tl.FocusedNode.ParentNode.Tag.ToString.Split(Char.Parse("_"))(0)
                    loadLoadDuLieuNhomNguoiDung(mNhom)
                    Dim tk As String = tl.FocusedNode.GetValue(0).ToString
                    grvNhom.ClearSelection()
                    For i As Integer = 0 To grvNhom.RowCount - 1
                        If grvNhom.IsMasterRow(i) AndAlso Not grvNhom.IsMasterRowEmpty(i) Then
                            Dim detailView As GridView = DirectCast(grvNhom.GetDetailView(i, 0), GridView)
                            detailView.ClearSelection()
                            If detailView IsNot Nothing Then
                                For j As Integer = 0 To detailView.RowCount - 1
                                    If detailView.GetRowCellValue(j, "TaiKhoan").ToString.Replace(" ", "") = tk Then
                                        detailView.SelectRow(j)
                                        _NhomND = False
                                        detailView.FocusedRowHandle = j
                                        Return
                                    End If
                                Next
                            End If
                        End If
                    Next

                End If
            End If
        End If


    End Sub


    Private Sub grvNhom_RowCellClick(sender As Object, e As RowCellClickEventArgs) Handles grvNhom.RowCellClick
        Dim ma As String = grvNhom.GetRowCellValue(0, "MaNhom").ToString
        For Each node As TreeListNode In treeNDVaiTro.Nodes
            If node.Tag.ToString = (ma + "_Nhom").ToString Then
                node.Selected = True
            End If
        Next
        _NhomND = True
        dsChucNang.Clear()
        treePhanQuyen.Nodes.Clear()
        layDuLieuPhanQuyen(ma)
    End Sub


    Private Sub grvNhom_MasterRowGetRelationName(sender As Object, e As MasterRowGetRelationNameEventArgs) Handles grvNhom.MasterRowGetRelationName
        e.RelationName = "gridViewDetail"
    End Sub

    Private Sub grvNguoiDung_RowCellClick(sender As Object, e As RowCellClickEventArgs) Handles grvNguoiDung.RowCellClick
        If e.RowHandle >= 0 Then
            For i As Integer = 0 To grvNhom.RowCount - 1
                If grvNhom.IsMasterRow(i) AndAlso Not grvNhom.IsMasterRowEmpty(i) Then
                    Dim detailView As GridView = DirectCast(grvNhom.GetDetailView(i, 0), GridView)

                    If detailView IsNot Nothing Then
                        Dim rowIndex As Integer() = detailView.GetSelectedRows()
                        Dim taiKhoan As String = detailView.GetRowCellValue(rowIndex(0), "TaiKhoan").ToString.Replace(" ", "")
                        For Each node As TreeListNode In treeNDVaiTro.Nodes
                            For Each nodeChil As TreeListNode In node.Nodes
                                If nodeChil.GetValue(0).ToString = taiKhoan Then
                                    nodeChil.Selected = True
                                    Return
                                End If
                            Next
                        Next
                    End If
                End If
            Next
            _NhomND = False
        End If

        'Dim tk As String = grvNhom.GetRowCellValue(0, "TaiKhoan").ToString

    End Sub
End Class
